package space.yangmin.baidu.downloader.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.InputStream;

@Slf4j
public class DownloadUtil {

    public static File download(String url, String fileName, String parentDirectory){
        HttpResponse httpResponse = HttpRequest.get(url).setFollowRedirects(true).execute();
        log.info("下载文件响应：\n" + httpResponse.headers());
        InputStream inputStream = httpResponse.bodyStream();
        File file = FileUtil.writeFromStream(inputStream, parentDirectory + "/" + fileName);
        return file;
    }

}
